package com.easyhome.common.timer;

import com.alibaba.schedulerx.common.util.ConfigUtil;
import com.alibaba.schedulerx.worker.domain.WorkerConstants;
import com.alibaba.schedulerx.worker.logcollector.LogCollectorFactory;
import org.joda.time.DateTime;

import java.util.List;
import java.util.Map;

/**
 * 为了兼容原有schedulerx代码
 *
 * @author wangshufeng
 */
public final class JobContext {
    //基本信息
    private long jobId;
    private long jobInstanceId;
    private Long wfInstanceId;
    private long taskId = 0;
    private String jobName;
    private DateTime scheduleTime;
    private DateTime dataTime;
    private String executeMode;
    private String jobType;
    private String instanceMasterActorPath;
    private String taskName;
    private Object task;
    private String groupId;
    private String content;
    private String user;

    //jobInstance最大重试次数
    private int maxAttempt;
    //jobInstance当前重试次数
    private int attempt;

    // 任务自定义参数
    private String jobParameters;

    // 每次触发传递自定义参数，仅api触发可以传递
    private String instanceParameters;

    //所有子任务的结果Map<taskId, result>
    private Map<Long, String> taskResults;

    //task最大重试次数
    private int taskMaxAttempt;
    //task当前重试次数
    private int taskAttempt = 0;
    //task重试间隔(秒)
    private int taskAttemptInterval;

    //秒级任务循环次数
    private long serialNum;

    private Long shardingId;
    private String shardingParameter;

    public JobContext() {}

    public JobContext(String jobParameters) {
        this.jobParameters = jobParameters;
    }

    public long getJobId() {
        return jobId;
    }

    public long getJobInstanceId() {
        return jobInstanceId;
    }

    public Long getWfInstanceId() {
        return wfInstanceId;
    }

    public long getTaskId() {
        return taskId;
    }

    public String getJobName() {
        return jobName;
    }

    public DateTime getScheduleTime() {
        return scheduleTime;
    }

    public DateTime getDataTime() {
        return dataTime;
    }

    public String getExecuteMode() {
        return executeMode;
    }

    public String getJobType() {
        return jobType;
    }

    public String getContent() {
        return content;
    }

    public String getUser() {
        return user;
    }

    public String getJobParameters() {
        return jobParameters;
    }

    public String getInstanceParameters() {
        return instanceParameters;
    }

    public String getTaskMasterActorPath() {
        return getInstanceMasterActorPath();
    }

    public String getInstanceMasterActorPath() {
        return instanceMasterActorPath;
    }

    public String getTaskName() {
        return taskName;
    }

    public Object getTask() {
        return task;
    }

    public int getTaskMaxAttempt() {
        return taskMaxAttempt;
    }

    public int getTaskAttemptInterval() {
        return taskAttemptInterval;
    }

    public String getGroupId() {
        return groupId;
    }

    public Map<Long, String> getTaskResults() {
        return taskResults;
    }

    public int getMaxAttempt() {
        return maxAttempt;
    }

    public int getAttempt() {
        return attempt;
    }

    public long getSerialNum() {
        return serialNum;
    }

    public int getTaskAttempt() {
        return taskAttempt;
    }

    public Long getShardingId() {
        return shardingId;
    }

    public String getShardingParameter() {
        return shardingParameter;
    }

    public void setTaskAttempt(int taskAttempt) {
        this.taskAttempt = taskAttempt;
    }

    public static JobContext.JobContextBuilder newBuilder() {
        return new JobContext.JobContextBuilder();
    }

    public static JobContext.JobContextBuilder newBuilder(JobContext context) {
        return new JobContext.JobContextBuilder(context);
    }

    public static final class JobContextBuilder {
        private JobContext context = new JobContext();

        private JobContextBuilder() {}

        private JobContextBuilder(JobContext context) {
            this.context = context;
        }

        public JobContext.JobContextBuilder setJobId(long jobId) {
            this.context.jobId = jobId;
            return this;
        }

        public JobContext.JobContextBuilder setJobInstanceId(long jobInstanceId) {
            this.context.jobInstanceId = jobInstanceId;
            return this;
        }

        public JobContext.JobContextBuilder setWfInstanceId(long wfInstanceId) {
            this.context.wfInstanceId = wfInstanceId;
            return this;
        }

        public JobContext.JobContextBuilder setTaskId(long taskId) {
            this.context.taskId = taskId;
            return this;
        }

        public JobContext.JobContextBuilder setJobName(String jobName) {
            this.context.jobName = jobName;
            return this;
        }

        public JobContext.JobContextBuilder setScheduleTime(DateTime scheduleTime) {
            this.context.scheduleTime = scheduleTime;
            return this;
        }

        public JobContext.JobContextBuilder setDataTime(DateTime dataTime) {
            this.context.dataTime = dataTime;
            return this;
        }

        public JobContext.JobContextBuilder setExecuteMode(String executeMode) {
            this.context.executeMode = executeMode;
            return this;
        }

        public JobContext.JobContextBuilder setJobType(String jobType) {
            this.context.jobType = jobType;
            return this;
        }

        public JobContext.JobContextBuilder setContent(String content) {
            this.context.content = content;
            return this;
        }

        public JobContext.JobContextBuilder setUser(String user) {
            this.context.user = user;
            return this;
        }

        public JobContext.JobContextBuilder setJobParameters(String jobParameters) {
            this.context.jobParameters = jobParameters;
            return this;
        }

        public JobContext.JobContextBuilder setInstanceParameters(String instanceParameters) {
            this.context.instanceParameters = instanceParameters;
            return this;
        }

        public JobContext.JobContextBuilder setTaskMasterActorPath(String actorPath) {
            this.context.instanceMasterActorPath = actorPath;
            return this;
        }

        public JobContext.JobContextBuilder setTaskName(String taskName) {
            this.context.taskName = taskName;
            return this;
        }

        public JobContext.JobContextBuilder setTask(Object task) {
            this.context.task = task;
            return this;
        }

        public JobContext.JobContextBuilder setGroupId(String groupId) {
            this.context.groupId = groupId;
            return this;
        }


        public JobContext.JobContextBuilder setTaskResults(Map<Long, String> taskResults) {
            this.context.taskResults = taskResults;
            return this;
        }

        public JobContext build() {
            return context;
        }


    }

}